/**
 * Copyright 2007 Igor Chaves Cananéa
 *
 * This file is part of PBSFW
 *
 * PBSFW is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * PBSFW is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define _INTERNAL_CLASS_
#include "resource.h"
#include "simulator.h"

namespace pbsfw {
    Resource::Resource(string name, uint32_t maxAmount)
        : name(name), maxAmount(maxAmount), currentAmount(0) {
    }

    Resource::Resource(const Resource &r)
        : name(r.name), maxAmount(r.maxAmount), currentAmount(r.currentAmount) {
    }

    void Resource::release() {
        --this->currentAmount;

        uint size = this->waitQueue.size();

        for (uint32_t i = 0; i < size; ++i) {
            Process *proc = this->waitQueue.front();
            if (this->hasResource()) {
                this->waitQueue.pop_front();
                sim.reactivate(*proc, sim.now());
                ++this->currentAmount;
            }
            else {
                i = size;
            }
        }
    }
};
